package br.com.sgpv2.hibernate.evento;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import br.com.sgpv2.entity.evento.Evento;
import br.com.sgpv2.entity.movimentacao.Movimentacao;
import br.com.sgpv2.hibernate.infra.SGPGenericDAO;

@Repository(value="eventoRepository")
public class EventoHibernateDAO extends SGPGenericDAO<Evento> implements EventoRepository {
	
	/**
	 * Construtor padrão.
	 * 
	 */
	@Autowired
	public EventoHibernateDAO(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super(Evento.class);
		super.setSessionFactory(factory);
	}

	@Override
	public List<Movimentacao> listarMovimentacoesPorEvento(int idEvento) {
		Criteria criteria = createCriteria(Movimentacao.class);
		Criteria criteriaEventos = criteria.createCriteria("eventos");  
		criteriaEventos.add(Restrictions.eq("id", idEvento));

		return Collections.checkedList((List<Movimentacao>) criteria.list(),
				Movimentacao.class);
	}

	@Override
	public Evento buscarPorCodigo(String codigo) {
		Criteria criteria = createCriteria(Evento.class);
		criteria.add(Restrictions.eq("codigo", codigo));
		return (Evento) criteria.uniqueResult();
	}

	public List<Evento> listarOrder(String atributoOrder) {
		Criteria criteria = createCriteria(Evento.class);
		criteria.addOrder(Order.asc(atributoOrder));
		return Collections.checkedList((List<Evento>) criteria.list(),
				Evento.class);
	}
	
	public List<Evento> consultarPorDescricao(String descricao) {
		Criteria criteria = createCriteria(Evento.class);
		criteria.add(Restrictions.like("descricao", descricao, MatchMode.ANYWHERE));
		criteria.addOrder(Order.asc("descricao"));
		return Collections.checkedList((List<Evento>) criteria.list(),
				Evento.class);
	}

}
